<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="所属企业" prop="enterpriseInfo.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择所属企业"
            labelName = 'companyName'
            labelValue = 'id'
            :value = "inputForm.enterpriseInfo.id"
            :limit="1"
            @getValue='(value) => {inputForm.enterpriseInfo.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/list"
            entityBeanName="enterpriseInformation"
            queryEntityUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="流程" prop="processInfo.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择流程"
            labelName = 'processName'
            labelValue = 'id'
            :value = "inputForm.processInfo.id"
            :limit="1"
            @getValue='(value) => {inputForm.processInfo.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'processName',
              label: '流程名称'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'processName',
              label: '流程名称'
            }
            ]"
            dataListUrl="/dlyrl/hr/processmain/processMain/list"
            entityBeanName="processMain"
            queryEntityUrl="/dlyrl/hr/processmain/processMain/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="当前流程序号" prop="nowProcessNum"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nowProcessNum" placeholder="请填写当前流程序号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="当前审批状态" prop="nowApprovalStatus"
                :rules="[
                 ]">
                <el-select v-model="inputForm.nowApprovalStatus" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('auditstatus')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="当前审批角色" prop="nowApprovalRole.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择当前审批角色"
            labelName = 'roleName'
            labelValue = 'id'
            :value = "inputForm.nowApprovalRole.id"
            :limit="1"
            @getValue='(value) => {inputForm.nowApprovalRole.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'roleName',
              label: '角色名'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'roleName',
              label: '角色名'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriserole/enterpriseRoleInformation/list"
            entityBeanName="enterpriseRoleInformation"
            queryEntityUrl="/dlyrl/hr/enterpriserole/enterpriseRoleInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="当前审批用户" prop="nowapprovaluser.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择当前审批用户"
            labelName = 'userName'
            labelValue = 'id'
            :value = "inputForm.nowapprovaluser.id"
            :limit="1"
            @getValue='(value) => {inputForm.nowapprovaluser.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/list"
            entityBeanName="enterpriseUserInformation"
            queryEntityUrl="/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="当前审批时间" prop="nowApprovalTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.nowApprovalTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="下一步流程序号" prop="nextProcessNum"
                :rules="[
                  {required: true, message:'下一步流程序号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.nextProcessNum" placeholder="请填写下一步流程序号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="下一个审批角色" prop="nextApprovalRole.id"
                :rules="[
                  {required: true, message:'下一个审批角色不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择下一个审批角色"
            labelName = 'roleName'
            labelValue = 'id'
            :value = "inputForm.nextApprovalRole.id"
            :limit="1"
            @getValue='(value) => {inputForm.nextApprovalRole.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'roleName',
              label: '角色名'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'roleName',
              label: '角色名'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriserole/enterpriseRoleInformation/list"
            entityBeanName="enterpriseRoleInformation"
            queryEntityUrl="/dlyrl/hr/enterpriserole/enterpriseRoleInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="抄送用户" prop="nextApprovalUser.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择抄送用户"
            labelName = 'userName'
            labelValue = 'id'
            :value = "inputForm.nextApprovalUser.id"
            :limit="1"
            @getValue='(value) => {inputForm.nextApprovalUser.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/list"
            entityBeanName="enterpriseUserInformation"
            queryEntityUrl="/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="驳回原因" prop="noReason"
                :rules="[
                  {required: true, message:'驳回原因不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.noReason" placeholder="请填写驳回原因"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="离职时间" prop="leavingTime"
                :rules="[
                  {required: true, message:'离职时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.leavingTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="入职时间" prop="entryTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.entryTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="离职原因" prop="leavingReason"
                :rules="[
                  {required: true, message:'离职原因不能为空', trigger:'blur'}
                 ]">
          <el-input type="textarea" v-model="inputForm.leavingReason" placeholder="请填写离职原因"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                  {required: true, message:'备注信息不能为空', trigger:'blur'}
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="离职证明" prop="leavingcertificate"
                :rules="[
                  {required: true, message:'离职证明不能为空', trigger:'blur'}
                 ]">
              <el-upload ref="leavingcertificate"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/leavingprocess/leavingProcess`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                       inputForm.leavingcertificate = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.leavingcertificate = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="leavingcertificateArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        leavingcertificateArra: [],
        inputForm: {
          id: '',
          enterpriseInfo: {
            id: ''
          },
          processInfo: {
            id: ''
          },
          leavingReason: '',
          nowProcessNum: '',
          nowApprovalStatus: '',
          nowApprovalRole: {
            id: ''
          },
          nowapprovaluser: {
            id: ''
          },
          nowApprovalTime: '',
          nextProcessNum: '',
          nextApprovalRole: {
            id: ''
          },
          nextApprovalUser: {
            id: ''
          },
          noReason: '',
          remarks: '',
          leavingTime: '',
          entryTime: '',
          leavingcertificate: ''
        }
      }
    },
    components: {
      GridSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建离职申请`
        } else if (method === 'edit') {
          this.title = '修改离职申请'
        } else if (method === 'view') {
          this.title = '查看离职申请'
        }
        this.leavingcertificateArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/leavingprocess/leavingProcess/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.leavingProcess)
              this.inputForm.leavingcertificate.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.leavingcertificateArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/leavingprocess/leavingProcess/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>